package com.awspaass.user.apps.syncwechataddress;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class EventClockInData implements IJob {
	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String date = datef.format(new Date());
		WeChatUtil wechat = new WeChatUtil();
		String cfgstr = wechat.readToString("E:\\Actionsoft\\bpm808\\bpm804\\apps\\install\\com.awspaas.user.apps.shhtaerospaceindustrial\\lib\\company.json");
		JSONArray companyArray = null;
		JSONObject jsonObject = JSON.parseObject(cfgstr);
		if (jsonObject != null) {
			// 取出按钮权限的数据
			companyArray = jsonObject.getJSONArray("companys");
		}
		Iterator<Object> num = companyArray.iterator();

		List sonRecords = new ArrayList();
		int totalParkNumber=0;
		while (num.hasNext()) {
			JSONObject company = (JSONObject) num.next();
			String c_wechatid = (String) company.get("companywechatid");
			String c_name = company.getString("name");
			String c_secret = (String) company.get("companywechatseret");
			String cksecret = (String) company.get("ckseret");
			int totalCkNumber = 0;
			if (!cksecret.equals("")) {
				BO boSonRecord = new BO();
				String access_token = wechat.getAccessToken(c_wechatid, c_secret);

				List<Map<String, String>> depsWechat = wechat.getDepartmentList(access_token);

				List<WxUserInfo> wechatuserlist = wechat.getDepartmentUserDetails(depsWechat, access_token, "0");
				int page = wechatuserlist.size() / 100 + 1;
				for (int i = 0; i < page; i++) {
					List t = new ArrayList();
					int max = 0;
					if (i + 1 == page) {
						max = wechatuserlist.size() % 100;
					} else {
						max = 100;
					}
					for (int j = 0; j < max; j++) {
						int index = i * 100 + j;
						WxUserInfo w = wechatuserlist.get(index);
						String id = w.getUserId();
						t.add(id);
					}
					totalCkNumber += wechat.getDKData(c_wechatid, cksecret, t);

				}
				boSonRecord.set("UNITNAME", c_name);
				boSonRecord.set("DAKAPSNNUM", totalCkNumber);
				boSonRecord.set("CALTIME", date);
				sonRecords.add(boSonRecord);
				totalParkNumber+=totalCkNumber;
				System.out.println(totalCkNumber);
			}

		}
		ProcessInstance processInst = SDK.getProcessAPI().createProcessInstance("obj_89a563adea1c4dba8af2836cd9ddc0ff",
				"admin", date + "预估就餐人数");
		BO boRecordData = new BO();
		boRecordData.set("TOTALDATE", date);
		boRecordData.set("TOTALPSNNUM",totalParkNumber);
		List recordDatas = new ArrayList();
		recordDatas.add(boRecordData);
		
		SDK.getBOAPI().create("BO_EU_SHSYST_RSTOTAL_HEAD", recordDatas, processInst, UserContext.fromUID("admin"));
		SDK.getBOAPI().create("BO_EU_SHSYST_RSTOTAL_BODY", sonRecords, processInst.getId(), "admin");
	}
}
